package com.buobao.bmall.controller;

import com.buobao.bmall.common.ApiRestResponse;
import com.buobao.bmall.common.Contents;
import com.buobao.bmall.exception.ExceptionEnum;
import com.buobao.bmall.model.pojo.User;
import com.buobao.bmall.model.request.LoginRequest;
import com.buobao.bmall.model.request.UserUpdateRequest;
import com.buobao.bmall.service.UserService;
import com.buobao.bmall.utils.MD5Util;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController  //restcontroller 相当于controller+responsebody
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/userinfo")
    public User personalPage(Integer userId){
        return userService.selectUserById(userId);
    }


    @PostMapping("/login")
    public ApiRestResponse login(@RequestBody LoginRequest loginRequest, HttpSession session) {
        if (StringUtils.isEmpty(loginRequest.getUserName())) {
            return ApiRestResponse.error(ExceptionEnum.NEED_USER_NAME);
        } else if (StringUtils.isEmpty(loginRequest.getPassword())) {
            return ApiRestResponse.error(ExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.selectUserByName(loginRequest.getUserName());
        String passwordMD5 = MD5Util.md5Digest(loginRequest.getPassword());
        if (!passwordMD5.equals(user.getPassword())) {
            return ApiRestResponse.error(ExceptionEnum.WRONG_PASSWORD);
        }
        session.setAttribute(Contents.CURRENT_USER, user);
        return ApiRestResponse.success(user);
    }

    @PostMapping("/register")
    public ApiRestResponse register(@RequestBody LoginRequest loginRequest) {
        if (StringUtils.isEmpty(loginRequest.getUserName())) {
            return ApiRestResponse.error(ExceptionEnum.NEED_USER_NAME);
        } else if (StringUtils.isEmpty(loginRequest.getPassword())) {
            return ApiRestResponse.error(ExceptionEnum.NEED_PASSWORD);
        } else if (loginRequest.getPassword().length()<8) {
            return ApiRestResponse.error(ExceptionEnum.SHORT_PASSWORD);
        }
        User user = userService.selectUserByName(loginRequest.getUserName());
        if (user!=null) {
            return ApiRestResponse.error(ExceptionEnum.USER_EXIST);
        }

        User insertUser = new User();
        insertUser.setUsername(loginRequest.getUserName());
        insertUser.setPassword(MD5Util.md5Digest(loginRequest.getPassword()));
        insertUser.setCreateTime(new Date());
        insertUser.setUpdateTime(new Date());
        insertUser.setPersonalizedSignature(loginRequest.getPersonalizedSignature());
        insertUser.setRole(loginRequest.getRole());
        Integer result = userService.insertUser(insertUser);
        if (result == 1) {
            return ApiRestResponse.success();
        } else  {
            return ApiRestResponse.error(ExceptionEnum.REGISTER_FAILED);
        }
    }

    @PostMapping("/user/update")
    public ApiRestResponse updateUserInfo(HttpSession session, @RequestBody UserUpdateRequest userUpdateRequest) {
        User currentUser = (User) session.getAttribute(Contents.CURRENT_USER);
        currentUser.setPersonalizedSignature(userUpdateRequest.getSignature());
        Integer integer = userService.updateByPrimaryKey(currentUser);
        if (integer==1) {
            return ApiRestResponse.success();
        } else {
            return ApiRestResponse.error(ExceptionEnum.UPDATE_USER_FAILED);
        }
    }


    @PostMapping("/logout")
    public ApiRestResponse logout(HttpSession session){
        session.removeAttribute(Contents.CURRENT_USER);
        return ApiRestResponse.success();
    }

    @PostMapping("/adminlogin")
    public ApiRestResponse adminLogin(@RequestBody LoginRequest loginRequest, HttpSession session) {
        if (StringUtils.isEmpty(loginRequest.getUserName())) {
            return ApiRestResponse.error(ExceptionEnum.NEED_USER_NAME);
        } else if (StringUtils.isEmpty(loginRequest.getPassword())) {
            return ApiRestResponse.error(ExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.selectUserByName(loginRequest.getUserName());
        String passwordMD5 = MD5Util.md5Digest(loginRequest.getPassword());
        if (!passwordMD5.equals(user.getPassword())) {
            return ApiRestResponse.error(ExceptionEnum.WRONG_PASSWORD);
        }
        if (userService.checkAdmin(user)) {
            user.setPassword("");
            session.setAttribute(Contents.CURRENT_USER, user);
            return ApiRestResponse.success(user);
        } else {
            return ApiRestResponse.error(ExceptionEnum.NEED_ADMIN);
        }

    }
}
























